home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / misc / sci / ephem_src_4_28.lha / time.c < prev    next >
C/C++ Source or Header  |  1992-04-17  |  3KB  |  129 lines

  1. /* get the time from the os.
  2.  *
  3.  * here are two methods I was able to verify; pick one for your system and
  4.  *   define exactly one of TZA or TZB:
  5.  * TZA works on our ibm-pc/turbo-c and at&t systems,
  6.  * TZB works on our 4.2 BSD vax.
  7.  *
  8.  * I'm told that on Sun OS 4.0.3 (BSD 4.3?) and Apollo SR 10.1 TZB works if
  9.  *   you use <sys/time.h> in place of <time.h>.
  10.  * 
  11.  * On VMS, you DON'T want to define EITHER TZA nor TZB since it can't handle
  12.  *   time zones, period. time_fromsys() will detect that fact based on gmtime()
  13.  *   returning 0.
  14.  */
  15.  
  16. #define    TZA
  17.  
  18. #ifdef VMS
  19. #undef TZA
  20. #undef TZB
  21. #endif
  22.  
  23. #include <stdio.h>
  24. #include <time.h>
  25.  
  26. #include "astro.h"
  27. #include "circum.h"
  28.  
  29. extern char *strncpy();
  30. #ifndef VMS
  31. extern long time();
  32. #endif
  33.  
  34. static long c0;
  35. static double mjd0;
  36.  
  37. /* save current mjd and corresponding system clock for use by inc_mjd().
  38.  * this establishes the base correspondence between the mjd and system clock.
  39.  */
  40. set_t0 (np)
  41. Now *np;
  42. {
  43.     mjd0 = mjd;
  44.     (void) time (&c0);
  45. }
  46.  
  47. /* fill in n_mjd/tz/tznm from system clock.
  48.  */
  49. time_fromsys (np)
  50. Now *np;
  51. {
  52.     extern struct tm *gmtime(), *localtime();
  53.     struct tm *tp;
  54.     long c;
  55.     double day, hr;
  56.  
  57.     (void) time (&c);
  58.  
  59.     tp = gmtime (&c);
  60.     if (tp) {
  61.         cal_mjd (tp->tm_mon+1, (double)tp->tm_mday, tp->tm_year+1900, &day);
  62.         sex_dec (tp->tm_hour, tp->tm_min, tp->tm_sec, &hr);
  63.         mjd = day + hr/24.0;
  64.         tp = localtime (&c);
  65.         settzstuff (tp->tm_isdst ? 1 : 0, np);
  66.     } else {
  67.         /* if gmtime() doesn't work, we assume the timezone stuff won't
  68.          * either, so we just use what it is and leave it alone. Some
  69.          * systems (like VMS) do not know about time zones, so this is the
  70.          * best guess in that case.
  71.          */
  72.         tp = localtime (&c);
  73.         cal_mjd (tp->tm_mon+1, (double)tp->tm_mday, tp->tm_year+1900, &day);
  74.         sex_dec (tp->tm_hour, tp->tm_min, tp->tm_sec, &hr);
  75.         mjd = day + hr/24.0 + tz/24.0;
  76.     }
  77. }
  78.  
  79. /* given whether dst is now in effect (must be strictly 0 or 1), fill in
  80.  * tzname and tz within np.
  81.  */
  82. static
  83. settzstuff (dst, np)
  84. int dst;
  85. Now *np;
  86. {
  87. #ifdef TZA
  88.     extern long timezone;
  89.     extern char *tzname[2];
  90.  
  91.     tzset();
  92.     tz = timezone/3600;
  93.     if (dst)
  94.         tz -= 1.0;
  95.     (void) strncpy (tznm, tzname[dst], sizeof(tznm)-1);
  96. #endif
  97. #ifdef TZB
  98.     extern char *timezone();
  99.     struct timeval timev;
  100.     struct timezone timez;
  101.  
  102.     gettimeofday (&timev, &timez);
  103.     tz = timez.tz_minuteswest/60;
  104.     if (dst)
  105.         tz -= 1.0;
  106.     (void) strncpy (tznm, timezone(timez.tz_minuteswest, dst),
  107.                                 sizeof(tznm)-1);
  108. #endif
  109.     tznm[sizeof(tznm)-1] = '\0';    /* insure string is terminated */
  110. }
  111.  
  112. inc_mjd (np, inc)
  113. Now *np;
  114. double inc;
  115. {
  116.     if (inc == RTC) {
  117.         long c;
  118.         (void) time (&c);
  119.         mjd = mjd0 + (c - c0)/SPD;
  120.     } else
  121.         mjd += inc/24.0;
  122.  
  123.     /* round to nearest whole second.
  124.      * without this, you can get fractional days so close to .5 but
  125.      * not quite there that mjd_hr() can return 24.0
  126.      */
  127.     rnd_second (&mjd);
  128. }
  129.